package com.xiaoshuidi.cloud.module.report.service.datamanage;

import com.xiaoshuidi.cloud.module.report.controller.datamanage.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.datamanage.RoomStatisticsMapper;
import com.xiaoshuidi.cloud.module.report.utils.ApartmentUtils;
import com.xiaoshuidi.cloud.module.report.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomStatisticsServiceImpl implements RoomStatisticsService{

    @Resource
    private RoomStatisticsMapper roomStatisticsMapper;
    @Resource
    private ApartmentUtils apartmentUtils;

    @Override
    public void insertData() {
        roomStatisticsMapper.insertData();
    }

    /**
     * 查询空置房数量信息
     * @param apartmentIds 门店id
     * @param includeModelMaintenanceRooms 是否包含样板维修房
     * @return
     */
    @Override
    public RmptyRoomStatisticsRespVO emptyRoomStatistics(List<Long> apartmentIds, Boolean includeModelMaintenanceRooms) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        return roomStatisticsMapper.emptyRoomStatistics(apartmentIds, includeModelMaintenanceRooms);
    }

    /**
     * 空置率统计
     *
     * @param apartmentIds 门店id
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public EmptyRoomRateStatisticsRespVO emptyRoomRateStatistics(List<Long> apartmentIds, @NotNull LocalDate startDate, @NotNull LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        List<RoomRateStatisticsVO> roomRateStatisticsVOS = roomStatisticsMapper.emptyRoomRateStatistics(apartmentIds, startDate, endDate);
        BigDecimal currentEmptyRoomRate = roomStatisticsMapper.currentEmptyRoomRate(apartmentIds);
        List<String> yearMonthList = DateUtils.getYearMonthListByStartAndEndDate(startDate, endDate);
        List<RoomRateStatisticsVO> respVOS = yearMonthList.stream().map(yearMonth -> {
            RoomRateStatisticsVO respVO = new RoomRateStatisticsVO();
            respVO.setMonth(yearMonth);
            roomRateStatisticsVOS.stream().filter(p -> yearMonth.equals(p.getMonth())).findFirst().ifPresent(f -> {
                respVO.setEmptyRoomRate(f.getEmptyRoomRate());
            });
            return respVO;
        }).collect(Collectors.toList());
        return new EmptyRoomRateStatisticsRespVO().setEmptyRoomRate(respVOS).setCurrentEmptyRoomRate(currentEmptyRoomRate);
    }

    @Override
    public EmptyRoomDamagesStatisticsRespVO emptyRoomDamagesStatistics(List<Long> apartmentIds, String condition, LocalDate startDate, LocalDate endDate) {
        apartmentIds = apartmentUtils.getAllIdsWhenIdsIsNull(apartmentIds);
        List<DamagesStatistics> damagesStatistics = roomStatisticsMapper.emptyRoomDamagesStatistics(apartmentIds, condition, startDate, endDate);
        BigDecimal decimal = BigDecimal.ZERO;
        for (DamagesStatistics damagesStatistic : damagesStatistics) {
            decimal = decimal.add(damagesStatistic.getDamagesRental());
        }
        List<String> yearMonthList = DateUtils.getYearMonthListByStartAndEndDate(startDate, endDate);
        List<DamagesStatistics> respVOS = yearMonthList.stream().map(yearMonth -> {
            DamagesStatistics respVO = new DamagesStatistics();
            respVO.setMonthGroup(yearMonth);
            damagesStatistics.stream().filter(p -> yearMonth.equals(p.getMonthGroup())).findFirst().ifPresent(f -> {
                respVO.setDamagesRental(f.getDamagesRental());
            });
            return respVO;
        }).collect(Collectors.toList());

        return new EmptyRoomDamagesStatisticsRespVO().setDamagesStatisticsList(respVOS).setTotalDamagesRental(decimal);
    }




}
